unit uFrmPlayerWin;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, ExtCtrls, StdCtrls, gnugettext, Oracle, usqls, umainutils, uprintwin;

type
  TFrmPlayerWin = class(TForm)
    img_Player: TImage;
    btn_Cancel: TButton;
    btn_Ok: TButton;
    lbl_PlayerName: TLabel;
    edt_WinValue: TLabeledEdit;
    Label1: TLabel;
    chb_Split: TCheckBox;
    pnl_Split: TPanel;
    com_SplitFrom: TComboBox;
    com_SplitTo: TComboBox;
    Label3: TLabel;
    Label4: TLabel;
    procedure FormKeyPress(Sender: TObject; var Key: Char);
    procedure FormShow(Sender: TObject);
    procedure btn_CancelClick(Sender: TObject);
    procedure btn_OkClick(Sender: TObject);
    procedure chb_SplitClick(Sender: TObject);
    procedure com_SplitChange(Sender: TObject);
  private
    { Private declarations }
    RestMoneyPrize: string;
    m_TicketID, m_TournamentID: string;
    function GetWinPositionValue(TournamentID, WinPos: string): double;
  public
    procedure ShowWinPlayerForm(TournamentID, TicketID, WinPos: string);
    { Public declarations }
  end;

var
  FrmPlayerWin: TFrmPlayerWin;

implementation

uses ufrmCageTournament, uFrmMain;
{$R *.dfm}
{ TFrmPlayerWin }

procedure TFrmPlayerWin.btn_CancelClick(Sender: TObject);
begin
  Close();
end;

procedure TFrmPlayerWin.btn_OkClick(Sender: TObject);
begin
  if MessageDlg(_('Confirm payment:') + ' ' + edt_WinValue.Text + ' ' + Currency_Symbol, mtConfirmation, [mbYes, mbNo], 0) = mrYes then
  begin
    if StrToFloat(edt_WinValue.Text) > StrToFloat(RestMoneyPrize) then
    begin
      ShowMessage(_('Win value is higher than remaining money prize!'));
    end
    else
    begin
      if chb_Split.Checked then
      begin
        UpdateDataToDB('TICKETS', 'WIN_POSITION=' + '''' + com_SplitFrom.Text + '''' + ',WIN_POS_SPLIT_TO=' + '''' + com_SplitTo.Text + '''', 'ID=' + m_TicketID);
      end;
      UpdateDataToDB('TICKETS', 'PAY_VALUE=' + '''' + edt_WinValue.Text + '''', 'ID=' + m_TicketID);
      FrmPrintWin.SetData(m_TicketID, OraSessMainUtils);
    end;
  end;
  Close();
end;

procedure TFrmPlayerWin.chb_SplitClick(Sender: TObject);
begin
  pnl_Split.Visible := chb_Split.Checked;
  com_SplitFrom.Text := '';
  com_SplitTo.Text := '';
end;

procedure TFrmPlayerWin.com_SplitChange(Sender: TObject);
var
  FromP, ToP, i, SumPos: integer;
  WinValue: double;
begin
  if com_SplitFrom.Text = '' then
    FromP := 1
  else
    FromP := StrToInt(com_SplitFrom.Text);
  if com_SplitTo.Text = '' then
    ToP := 1
  else
    ToP := StrToInt(com_SplitTo.Text);
  if FromP > ToP then
  begin
    com_SplitFrom.ItemIndex := com_SplitFrom.Items.IndexOf(inttostr(ToP));
    com_SplitChange(self);
    exit;
  end;
  WinValue := 0;
  for i := FromP to ToP do
  begin
    WinValue := WinValue + GetWinPositionValue(m_TournamentID, inttostr(i));
  end;
  SumPos := ToP - FromP + 1;
  edt_WinValue.Text := FloatToStr(WinValue / (SumPos));
end;

procedure TFrmPlayerWin.FormKeyPress(Sender: TObject; var Key: Char);
begin
  // The Escape key is #27.
  if Key = #27 then
    Close;
end;

procedure TFrmPlayerWin.FormShow(Sender: TObject);
begin
  img_Player.Picture := frmCageTournament.img_Player.Picture;
  lbl_PlayerName.Caption := frmCageTournament.stg_Players.Cells[1, frmCageTournament.stg_Players.Row];
end;

function TFrmPlayerWin.GetWinPositionValue(TournamentID, WinPos: string): double;
begin
  result := -1;
  FrmMain.OracleQuery1.Close;
  FrmMain.OracleQuery1.SQL.Clear;
  FrmMain.OracleQuery1.SQL.Text := 'SELECT WIN_VALUE FROM GET_WIN_VALUE WHERE TOURNAMENT_ID='+TournamentID+' AND SEQ_INDEX='+WinPos;
  FrmMain.OracleQuery1.Execute;
  if FrmMain.OracleQuery1.Eof then
  begin
    ShowMessage(_('Win position to small or win distribution not set!'));
    exit;
  end
  else
  begin
    result := FrmMain.OracleQuery1.Field(0);
  end;
end;

procedure TFrmPlayerWin.ShowWinPlayerForm(TournamentID, TicketID, WinPos: string);
var
  WinValue: double;
  i: integer;
begin
  m_TournamentID := TournamentID;
  chb_Split.Checked := false;
  pnl_Split.Visible := false;
  FrmMain.OracleQuery1.Close;
  FrmMain.OracleQuery1.SQL.Clear;
  FrmMain.OracleQuery1.SQL.Text := 'select money_prize-(select nvl(sum(pay_value),0) from TICKETS where deleted=0 and tournament_id=' + TournamentID +
    ') as REST_MONEY_PRIZE from GETTICKETTOURNAMENTS where id=' + TournamentID;
  FrmMain.OracleQuery1.Execute;
  RestMoneyPrize := '0';
  if not FrmMain.OracleQuery1.Eof then
  begin
    RestMoneyPrize := FrmMain.OracleQuery1.Field(0);
  end;
  // --------------------
  m_TicketID := TicketID;
  Label1.Caption := Currency_Symbol;
  WinValue := GetWinPositionValue(TournamentID, WinPos);
  if WinValue <> -1 then
  begin
    edt_WinValue.Text := FloatToStr(WinValue);
    // get wining positions
    FrmMain.OracleQuery1.Close;
    FrmMain.OracleQuery1.SQL.Clear;
    FrmMain.OracleQuery1.SQL.Text := 'SELECT TO_NUM FROM GET_WIN_TO_NUM WHERE TOURNAMENT_ID=' + m_TournamentID;
    FrmMain.OracleQuery1.Execute;
    com_SplitFrom.Items.Clear;
    com_SplitTo.Items.Clear;
    for i := 1 to FrmMain.OracleQuery1.Field(0) do
    begin
      com_SplitFrom.Items.Add(inttostr(i));
      com_SplitTo.Items.Add(inttostr(i));
    end;
    showmodal;
  end;
end;

end.
